/// <summary>
/// Camera follow.
/// this script use for control main camera to follow target(character)
/// </summary>

using UnityEngine;
using System.Collections;

[System.Serializable]
public class ShakeCameraData
{
    public Vector3 shakeAmount = Vector3.one;
    public Vector3 rotationAmount = Vector3.one;
	public float shakeDuration = 0.5f;
}

[System.Serializable]
public class CameraPositionData
{
	public CameraPositionData(float angle, float distance, float height)
	{
		this.angle = angle;
		this.distance = distance;
		this.height = height;
	}

	public float angle;
	public float distance;
	public float height;
}

public enum CameraType
{
    BIRDVIEW,
    FOLLOW,
    BATTLE,
    NEARBY
}

public class CameraFollow : MonoBehaviour 
{
    public Transform target; //Target to follow
	public float angle = 15; //Angle camera
	public float distance = -4; //Distance target
	public float height = 2.5f; // Height camera	
    public float followSpeed = 3.0f;
	public ShakeCameraData iTweenShakeData;

	//Private variable field
    public Vector3 posCamera;
	private Vector3 angleCam;
	public static CameraFollow instance;
	public CameraType type = CameraType.FOLLOW;
	public CameraPositionData birdviewPosition = new CameraPositionData(60, -5, 10); 
	public CameraPositionData followPosition = new CameraPositionData(41, -5, 7);
	public CameraPositionData battlePosition = new CameraPositionData(37, -6, 6);
	public CameraPositionData nearbyPosition = new CameraPositionData(22, -2, 3); 
	
	void Start(){
        instance = this;	
        posCamera = transform.position;
        cam = GetComponentInChildren<Camera>();
        GameObject[] heroes = GameObject.FindGameObjectsWithTag("Hero");
        foreach(GameObject go in heroes)
        {
            if(go.activeSelf)
            {
                SetTarget(go.transform);
            }
        }
    }

	public void SetTarget(Transform target)
	{
		this.target = target;
	}

	public void SetPosition(float angle,float distance,float height)
	{
		this.angle = angle;
		this.distance = distance;
		this.height = height;
	}
	
	void LateUpdate(){

		if(target)
        {
			posCamera.x = Mathf.Lerp(posCamera.x, target.position.x, followSpeed * Time.deltaTime);
			posCamera.y = Mathf.Lerp(posCamera.y, target.position.y + height, followSpeed * Time.deltaTime);
			posCamera.z = Mathf.Lerp(posCamera.z, target.position.z + distance,followSpeed *  Time.deltaTime);
			transform.position = posCamera;
			angleCam.x = angle;
			angleCam.y = Mathf.Lerp(angleCam.y, 0, 1 * Time.deltaTime);
			angleCam.z = transform.eulerAngles.z;
			transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, angleCam, 1 * Time.deltaTime);

            Camera.main.transform.localPosition = Vector3.Lerp(Camera.main.transform.localPosition,Vector3.zero,0.8f);
		}
    }

    public Camera cam;
    public float hitTime;
    public float  startingShakeDistance = 0.8f;
    public float  decreasePercentage = 0.5f;
    public int shakeCounter = 0;
    public int maxShakeCounter = 3;
    public float shakeSpeed = 55.0f;
    public float shakeDistance = 0.8f;
    public float maxShakeDistance = 0.8f;

    void DoShakeCameraMain()
    {
        iTween.ShakeRotation(Camera.main.gameObject, iTweenShakeData.rotationAmount, iTweenShakeData.shakeDuration);
        iTween.ShakePosition(Camera.main.gameObject, iTweenShakeData.shakeAmount, iTweenShakeData.shakeDuration);
    }

	public void Shake(ShakeCameraData shakeData)
    {
        iTweenShakeData = shakeData;
        DoShakeCameraMain();
    }

    void ResetCamera()
    {
        Camera.main.transform.localPosition = Vector3.zero;
        Camera.main.transform.localRotation = Quaternion.identity;
    }

    #region camera view data set
    public void SetCameraType(CameraType type)
    {
        this.type = type;
        switch (type)
        {
            case CameraType.BIRDVIEW:
                angle = birdviewPosition.angle; 
				distance = birdviewPosition.distance; 
				height = birdviewPosition.height;
                break;
            case CameraType.FOLLOW:
                angle = followPosition.angle; 
                distance = followPosition.distance; 
                height = followPosition.height;    
                break;
            case CameraType.BATTLE:
                angle = battlePosition.angle; 
				distance = battlePosition.distance; 
				height = battlePosition.height;   
                break;
            case CameraType.NEARBY:
				angle = nearbyPosition.angle; 
				distance = nearbyPosition.distance; 
				height = nearbyPosition.height; 
                break;
        }
    }
    #endregion

}
